<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>
<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>
<script src="/static/product/scripts/common.js"></script>
<script>
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    $(function () {
        pageInit();
    });
    function pageInit(){
        //下拉框初始化
        schoolSelectInit('{{.m.School.Id}}');
        gradeSelectInit('{{.m.Grade}}');
        statusSelectInit('{{.m.Status}}');
        adressSelectInit('{{.m.Adress}}');
        studentSchoolSelectInit('{{.m.StudentSchool.Id}}');
        relateSelectInit('{{.m.Relate}}');
        //附值
        //验证
        validateInfo(); 
    }
    // 校区下拉框初始化
    function schoolSelectInit(selecteds) {
        var url = '{{urlfor "student/SchoolController.DataList"}}';
        var $select = $("#School");
        $select.html('');
        $.sdpost(url,{},function (re) {
            if(re.code === 0) {
                var html=[];
                $(re.obj).each(function(i,e){
                    html.push('<option value="' + e.id + '">' + e.name + '</option>')
                });
                $select.html(html.join(''));
                $select.selectpicker({liveSearch:true});
                $select.selectpicker('val',selecteds.split(','));
            }else {
                layer.alert("获取数据失败",{icon:2,title:'失败'})
            }
        })
    }
    // 学生地址框初始化
    function adressSelectInit(selecteds) {
        var url = '{{urlfor "StudentAreaController.DataList"}}';
        var $select = $("#Adress");
        $select.html('');
        $.sdpost(url,{},function (re) {
            if(re.code === 0) {
                var html=[];
                $(re.obj).each(function(i,e){
                    html.push('<option value="' + e.name + '">' + e.name + '</option>')
                });
                $select.html(html.join(''));
                $select.selectpicker({liveSearch:true});
                $select.selectpicker('val',selecteds.split(','));
            }else {
                layer.alert("获取数据失败",{icon:2,title:'失败'})
            }
        })
    }
    // 学生学校下拉框初始化
    function studentSchoolSelectInit(selecteds) {
        var url = '{{urlfor "student/StudentSchoolController.DataList"}}';
        var $select = $("#StudentSchoolId");
        $select.html('');
        $.sdpost(url,{},function (re) {
            if(re.code === 0) {
                var html=[];
                $(re.obj).each(function(i,e){
                    html.push('<option value="' + e.id + '">' + e.name + '</option>')
                });
                $select.html(html.join(''));
                $select.selectpicker({liveSearch:true});
                $select.selectpicker('val',selecteds.split(','));
            }else {
                layer.alert("获取数据失败",{icon:2,title:'失败'})
            }
        })
    }
    function gradeSelectInit(selecteds) {
        var $select = $("#Grade");
        $select.html('');
        var grade = gradeMap;
        var html = [];
        Object.keys(grade).forEach(function(key){
            html.push('<option value="' + key + '">' + grade[key] + '</option>');
        });
        $select.html(html.join(''));
        $select.selectpicker({liveSearch:true});
        $select.selectpicker('val',selecteds.split(','));
    }
    function statusSelectInit(selecteds) {
        var $select = $("#Status");
        $select.html('');
        var obj = studentStatusMap;
        var html = [];
        Object.keys(obj).forEach(function(key){
            html.push('<option value="' + key + '">' + obj[key] + '</option>');
        });
        $select.html(html.join(''));
        $select.selectpicker({liveSearch:true});
        $select.selectpicker('val',selecteds.split(','));
    }
    function relateSelectInit(selecteds) {
        var $select = $("#Relate");
        $select.html('');
        var obj = relateMap;
        var html = [];
        $(obj).each(function(i,e){
            html.push('<option value="' + e + '">' + e + '</option>')
        });
        $select.html(html.join(''));
        $select.selectpicker({liveSearch:true});
        $select.selectpicker('val',selecteds.split(','));
    }
    //正则表达式验证
    jQuery.validator.methods.mobile = function (value, element, param) {
        //如果为空，直接过
        if (value.length == 0) return true;
        if (/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/.test(value)) {
            return true;
        } else {
            return false;
        }
    }
    //表单验证及AJAX提交
    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                Name: {
                    maxlength: 24,
                    required: true
                },
                Sex: {
                    maxlength: 5,
                    required: true
                },
                Grade: {
                    maxlength: 16,
                    required: true,
                },
                Guarder: {
                    maxlength: 16,
                    required: true,
                },
                Relate: {
                    maxlength: 16,
                    required: true,
                },
                Contact1: {
                    maxlength: 16,
                    required: true,
                },
                Adress: {
                    maxlength: 16,
                    required: true,
                },
                SchoolId: {
                    maxlength: 16,
                    required: true,
                },
                StudentSchoolId: {
                    maxlength: 16,
                    required: true,
                },
                Status: {
                    maxlength: 16,
                    required: true,
                },
            },
            messages: {
                Mobile: {
                    mobile: "请输入正确的手机号",
                },
            },
            submitHandler: function (form) {
				var url ='{{ urlfor "StudentController.Edit"}}';
				var data = $thisForm.serialize();				
				$.sdpost(url,data,function(re){
					if(re.code === 0){
						parent.layer.msg(re.msg);
						parent.refresh(re.obj);
						parent.layer.close(index);
					}else{
						parent.layer.alert(re.msg,{icon:2,title:'失败'})
					}
				});
               return false;
            }
        });
    }

    function FormSubmit() {
        $("#thisForm").submit();		
    }
</script>